# Maintainer: Konstantin Gizdov < arch at kge dot pw >
# Contributor: Jan Kašpar < jan.kaspar at gmail dot com >
# Contributor: Alex Pearce < alex at alexpearce dot me >
pkgname=xrootd
pkgdesc="Software framework for fast, low latency, scalable and fault tolerant data access."
pkgver=5.5.5
pkgrel=1
arch=('loong64' 'x86_64')
url="https://xrootd.slac.stanford.edu/"
license=('LGPL3')
depends=('curl' 'fuse2' 'json-c' 'libxml2' 'openssl' 'perl' 'python' 'systemd'
         'python-setuptools' 'tinyxml' 'davix')
makedepends=('cmake' 'git' 'python-pip')
checkdepends=('cppunit')
source=(
    "${pkgname}-${pkgver}::git+https://github.com/${pkgname}/${pkgname}.git#tag=v${pkgver}"
    'cxx17.patch'
    'add_missing_header.patch'
)
sha256sums=('SKIP'
            'a6c7a9fc57e0e357ec9b993acb192a24f4839fd26e8a11278703033433c208ea'
            '2e1f604e8006c6d6634fb7e3cd9b17d321c2d0da5ce12b2db1b0dd97871dfefe')

get_pyver () {
    python -c 'import sys; print(str(sys.version_info[0]) + "." + str(sys.version_info[1]))'
}

prepare () {
    cd "${srcdir}/${pkgbase}-${pkgver}"
    # source tarballs don't seem to contain all submodules anymore
    git submodule update --init --recursive
    patch -Np1 -i "${srcdir}/cxx17.patch"
    patch -Np1 -i "${srcdir}/add_missing_header.patch"
}

build() {
    cd "${srcdir}/${pkgbase}-${pkgver}"

    # configure
    mkdir -p "${srcdir}/build"
    cd "${srcdir}/build"

    CFLAGS="${CFLAGS}" \
    CXXFLAGS="${CXXFLAGS}" \
    LDFLAGS="${LDFLAGS}" \
    cmake -DCMAKE_BUILD_TYPE:STRING=Release \
          -DCMAKE_INSTALL_LIBDIR:PATH=lib \
          -DCMAKE_INSTALL_PREFIX:PATH=/usr \
          -DCMAKE_CXX_STANDARD="17" \
          -DWITH_OPENSSL3=TRUE \
          -DENABLE_TESTS=1 \
          -DXRD_PYTHON_REQ_VERSION=$(get_pyver) \
          "${srcdir}/${pkgbase}-${pkgver}"

    # build
    make
}

check() {
    cd "${srcdir}/build/tests"

    # Check has significantly changed, to-be-updated
    # ./common/text-runner ./XrdCephTests/libXrdCephTests.so "All Tests"

    ##
    # This requires a running XRootD server with multiIP DNS forwarder and local disk servers
    # only run this if you have configured the env correctly,
    # examples in https://github.com/xrootd/xrootd-test-suite
    # sample environment can be configured like so:
    # export XRDTEST_MAINSERVERURL=metaman.xrd.test
    # or export XRDTEST_MAINSERVERURL=http://xrootd.cern.ch/
    # export XRDTEST_DISKSERVERURL=srv1.xrd.test
    # or export XRDTEST_DISKSERVERURL=http://xrootd.cern.ch/
    # export XRDTEST_DATAPATH=/tests/test-files/
    # export XRDTEST_LOCALFILE=/data/a048e67f-4397-4bb8-85eb-8d7e40d90763.dat
    # or export XRDTEST_LOCALFILE=/tmp/accwe.root
    # export XRDTEST_REMOTEFILE=${XRDTEST_MAINSERVERURL}${XRDTEST_DATAPATH}/a048e67f-4397-4bb8-85eb-8d7e40d90763.dat
    # or export XRDTEST_MULTIIPSERVERURL=multiip.xrd.test
    # ./common/text-runner ./XrdClTests/libXrdClTests.so "All Tests"
}

package() {
    cd "${srcdir}/build"
    make DESTDIR="${pkgdir}" install
    # drop unneeded test bin & lib
    rm "${pkgdir}/usr/bin/test-runner"
    rm "${pkgdir}/usr/lib/libXrdClTests.so"
    install -d "${pkgdir}/usr/lib/cmake/XRootD"
    mv "${pkgdir}/usr/share/xrootd/cmake/XRootDConfig.cmake" "${pkgdir}/usr/lib/cmake/XRootD"/
    rm -rf "${pkgdir}/usr/share/xrootd/cmake"
    ln -s "/usr/lib/cmake" "${pkgdir}/usr/share/xrootd/cmake"
}
